using System;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using CalcSharp.Core;

namespace CalcSharp
{
    public class ConsoleUI
    {
        private ConsoleUI() { }

        public static string DFileSize(Int64 fsize)
        {
            double outp;
            if (fsize > 1073741824)
            {
                outp = (fsize / 1073741824);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Gib";
            }
            else if (fsize > 1048576)
            {
                outp = (fsize / 1048576);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Mib";
            }
            else if (fsize > 1024)
            {
                outp = (fsize / 1024);
                outp = Math.Round(outp, 3);
                return outp.ToString() + " Kib";
            }
            else return fsize.ToString() + " byte";
        }
    }

    public class XMLMenu
    {
        private MenuStrip _target;
        private string _fname;

        public MenuStrip Target
        {
            get { return this._target; }
            set { this._target = value; }
        }
        public string Fname
        {
            get { return this._fname; }
            set { this._fname = value; }
        }

        private void AddMenuitem(XmlNode menunode, XmlNode parentnode)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            ToolStripItem[] parentitems;
            item.Name = menunode.Attributes["name"].Value;
            item.Text = menunode.Attributes["text"].Value;
            if ((parentnode != null) && (parentnode.Name == "menuitem"))
            {
                string key = parentnode.Attributes["name"].Value;
                parentitems = _target.Items.Find(key, true);
                item.ToolTipText = menunode.InnerText;
                item.Click += new EventHandler(item_Click);
                (parentitems[0] as ToolStripMenuItem).DropDownItems.Add(item);
            }
            else
            {
                _target.Items.Add(item);
            }
        }

        private void item_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem s = (ToolStripMenuItem)sender;
            string cmd = s.ToolTipText;
            if (!string.IsNullOrEmpty(cmd) && s.DropDownItems.Count == 0)
            {
                Program.ConsoleForm.ExecOneCommand(cmd);
            }
        }

        public void CreateMenu()
        {
            XmlDocument menudoc;
            XmlNode rootitem = null;
            bool ok;
            if (!string.IsNullOrEmpty(_fname) && (_target != null))
            {
                menudoc = new XmlDocument();
                menudoc.Load(this._fname);

                #region Locating ROOT Item (xmlmenu)
                ok = false;
                for (int i = 0; i < menudoc.ChildNodes.Count; i++)
                {
                    if (menudoc.ChildNodes[i].Name == "xmlmenu")
                    {
                        rootitem = menudoc.ChildNodes[i];
                        ok = true;
                        break;
                    }
                }
                if (!ok) throw new CSException("The specified xml file is not a valid menu XML");
                #endregion

                ArrayList subnodes = getsubnodes(rootitem);
                foreach (XmlNode i in subnodes)
                {
                    this.AddMenuitem(i, i.ParentNode);
                }
            }
            else throw new CSException("Fname and Target must be set up correctly");
        }

        private ArrayList getsubnodes(XmlNode startnode)
        {
            ArrayList output = new ArrayList();
            foreach (XmlNode i in startnode.ChildNodes)
            {
                if (i.Name == "menuitem")
                {
                    if (i.ChildNodes.Count == 0) output.Add(i);
                    else
                    {
                        output.Add(i);
                        output.AddRange(getsubnodes(i));
                    }
                }
            }
            return output;
        }

        public XMLMenu(string fname, MenuStrip t)
        {
            this._fname = fname;
            this._target = t;
            this.CreateMenu();
        }

    }
}